package Windows;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import Windows.Tree.TreeNodePic;

public class PicPanel extends JPanel implements MouseListener {
	private Image ientered;// 进入背景图片
	private Image iexited;// 离开背景图片
	public boolean entered = false;// 是否进入切换
	public final TreeNodePic treeNode = new TreeNodePic("", "Skin/index.png");
	private MouseListener mouse;

	public PicPanel() {
		this.setOpaque(false);
		this.addMouseListener(this);// 注册鼠标进入离开事件（切换背景）
	}

	public PicPanel(Image iexited) {
		this();
		this.iexited = iexited == null ? Toolkit.getDefaultToolkit().createImage("") : iexited;
	}

	public PicPanel(Image iexited, Image ientered) {
		this(iexited);
		this.ientered = ientered;
	}

	public PicPanel(String iexited) {
		this();
		this.iexited = Toolkit.getDefaultToolkit().createImage(iexited);
	}

	public PicPanel(String iexited, String ientered) {
		this(iexited);
		this.ientered = Toolkit.getDefaultToolkit().createImage(ientered);
	}

	/**
	 * 切换背景
	 * 
	 * @param entered
	 */
	public void actionEvent(boolean entered) {
		this.entered = entered;
		updateUI();
	}

	public Image getIexited() {
		return iexited;
	}

	public MouseListener getMouseListener() {
		if (mouse == null) {
			mouse = new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					PicPanel.this.entered = true;
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					PicPanel.this.entered = false;
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

			};
		}
		return mouse;
	}

	public TreeNodePic getTreeNode() {
		// TODO Auto-generated method stub
		return treeNode;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (ientered != null) {
			actionEvent(true);
		}
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (iexited != null) {
			actionEvent(false);
		}
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void paintComponent(Graphics g) {
		if (entered && ientered == null)
			entered = false;
		g.drawImage(entered ? ientered : iexited, 0, 0, this.getWidth(), this.getHeight(), this);
		super.paintComponent(g);
	}

	public JPanel setIexited(Image iexited) {
		this.iexited = iexited;
		return this;
	}

}
